(function () {
    'use strict';
    angular.module('module.business')
        .controller('RecordUintController', RecordUintController);
    RecordUintController.$inject = ['$rootScope', '$scope', '$filter', '$uibModal', '$http', '$state', 'i18nService'];

    function RecordUintController($rootScope, $scope, $filter, $uibModal, $http, $state, i18nService) {
        var vm = $scope;
        vm.activate = activate;     //初始化
        vm.loadTable = loadTable;//加载列表
        vm.auditsRecord = auditsRecord;
        vm.create = create;//新增弹窗
        vm.detail = detail;//查看弹窗
        vm.change = change;//变更弹窗
        vm.edit = edit;//编辑弹窗
        vm.systemInfo = systemInfo;//信息系统查看弹框
        vm.changeRecords = changeRecords;//变更记录弹窗
        vm.getSelected = getSelected; //获取所有选中的记录
        vm.selected=[];

        vm.gridOptions = {
            multiSelect: true,                                           //多选
            enableRowSelection: true,                                  //允许选中行
            //showGridFooter:true,
            enableRowHeaderSelection: false,//允许通过点击行首选中行
            enableColumnResizing : true,//设置宽度
            enableGridMenu: true,
            enableFullRowSelection: true,                              //允许通过点击行任意位置选中行
            modifierKeysToMultiSelect: true,                           //只允许通过键盘辅助多选(前提是允许多选)
            paginationPageSize: 10,
            paginationPageSizes: [10, 20, 50]

        };
        i18nService.setCurrentLang('zh-cn');
        vm.gridOptions.onRegisterApi = onRegisterApi;

        vm.gridOptions.columnDefs = [
            {field: 'number', visible: true, name: '序号',width:'50', pinnedLeft: true,cellTooltip: true,headerTooltip: true},
            {field: 'unitName', visible: true, name: '单位名称', pinnedLeft: true,cellTooltip: true,headerTooltip: true},
            {field: 'recordsTableNumber', visible: true, name: '备案表编号', width:'150',pinnedLeft: true,cellTooltip: true,headerTooltip: true},
            {field: 'subjectionRelationship', visible: true, name: '隶属关系', pinnedLeft: true,cellTooltip: true,headerTooltip: true},
            {field: 'unitType', visible: true, name: '单位类型', pinnedLeft: true,cellTooltip: true,headerTooltip: true},
            {field: 'industryType', visible: true, name: '行业类别', pinnedLeft: true,cellTooltip: true,headerTooltip: true},
            {field: 'unitConscientiousMan', visible: true, name: '单位负责人', pinnedLeft: true,cellTooltip: true,headerTooltip: true},
            {field: 'recordTime', visible: true, name: '备案日期', pinnedLeft: true,width:'150',cellTooltip: true,headerTooltip: true},
            {field: 'state', visible: true, name: '状态', pinnedLeft: true,width:'80',cellTooltip: true,headerTooltip: true},
        ];
        activate();
        return vm;

        function onRegisterApi(gridApi) {
            //set gridApi on scope
            vm.gridApi = gridApi;
        }
        function activate(){
            $scope.$on('$viewContentLoaded', function () {
                App.initAjax(); // initialize core components
            });

            $rootScope.settings.layout.pageContentWhite = true;
            $rootScope.settings.layout.pageBodySolid = true;
            $rootScope.settings.layout.pageSidebarClosed = false;

            loadTable();
        }

        function loadTable(){
            $http.get('app/module/business/record/unit/record-unit.json')
                .then(function(data){
                    vm.gridOptions.data = data.data.recordUnit;
                });
        }

        /**
         * 新增页面
         */
        function create(){
            var modalInstance = $uibModal.open({
                animation: true,
                ariaLabelledBy: 'modal-title',
                ariaDescribedBy: 'modal-body',
                templateUrl: 'create.html',
                controller: 'CreateController',
                controllerAs: 'create',
                size: 'lg'
            });
            modalInstance.result.then(function () {
                loadTable();
                vm.selected = [];//清空已选中的记录
            }, function () {

                loadTable();
                vm.selected = [];
            });
        }
        /**
         * 编辑页面
         */
        function edit(){
            vm.selected = getSelected();
            if(vm.selected.length !== 1){
                swal('请选择一条待编辑的记录！');
            } else {
                var modalInstance = $uibModal.open({
                    animation: true,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'edit.html',
                    controller: 'EditController',
                    controllerAs: 'edit',
                    size: 'lg'
                });
                modalInstance.result.then(function () {
                    loadTable();
                    vm.selected = [];//清空已选中的记录
                }, function () {

                    loadTable();
                    vm.selected = [];
                });
            }
        }
        /**
         * 查看页面
         */
        function detail(){
            vm.selected = getSelected();
            if(vm.selected.length !== 1){
                swal('请选择一条待查看的记录！');
            } else {
                var modalInstance = $uibModal.open({
                    animation: true,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'detail.html',
                    controller: 'DetailController',
                    controllerAs: 'detail',
                    size: 'lg'
                });
                modalInstance.result.then(function () {
                    loadTable();
                    vm.selected = [];//清空已选中的记录
                }, function () {

                    loadTable();
                    vm.selected = [];
                });
            }
        }

        /**
         * 变更弹窗
         */
        function change() {
            vm.selected = getSelected();
            if(vm.selected.length !== 1){
                swal('请选择一条待变更的记录！');
            } else {
                var modalInstance = $uibModal.open({
                    animation: true,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'change.html',
                    controller: 'ChangeController',
                    controllerAs: 'change',
                    size: 'lg'
                });
                modalInstance.result.then(function () {
                    loadTable();
                    vm.selected = [];//清空已选中的记录
                }, function () {

                    loadTable();
                    vm.selected = [];
                });
            }
        }
        /**
         * 变更记录弹窗
         */
        function changeRecords() {
                var modalInstance = $uibModal.open({
                    animation: true,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'changeRecord.html',
                    controller: 'ChangeRecordController',
                    controllerAs: 'changeRecord',
                    size: 'lg'
                });
                modalInstance.result.then(function () {

                }, function () {

                });
        }
        /**
         * 审核记录弹窗
         */
        function auditsRecord() {
                var modalInstance = $uibModal.open({
                    animation: true,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'auditsRecord.html',
                    controller: 'AuditsRecordController',
                    controllerAs: 'auditsRecord',
                    size: 'lg'
                });
                modalInstance.result.then(function () {

                }, function () {

                });
        }
        /**
         * 系统信息查看弹窗
         */
        function systemInfo() {
                var modalInstance = $uibModal.open({
                    animation: true,
                    ariaLabelledBy: 'modal-title',
                    ariaDescribedBy: 'modal-body',
                    templateUrl: 'systemInfo.html',
                    controller: 'SystemInfoController',
                    controllerAs: 'systemInfo',
                    size: 'lg'
                });
                modalInstance.result.then(function () {

                }, function () {

                });
        }
        /**
         * 获取选中记录的id
         * @returns {Array}
         */
        function getSelected(){
            var arr = vm.gridApi.selection.getSelectedRows();
            var selections = [];
            arr.forEach(function(e){
                selections.push(e.CBOX_INFO_GUID);
            });
            return selections;
        }

    }
})();